import 'package:dog_miao/utils/deviceSize.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:amap_location/amap_location.dart';


class Square extends StatefulWidget {
  Square({Key key}):super(key: key);

  @override
  _Square createState() => _Square();
}

class _Square extends State<Square> with SingleTickerProviderStateMixin {
  String city = "成都";
  TabController _tabController;

  @override
  void initState() {
    AMapLocationClient.setApiKey("1bbe2a560a9543ca71cbaabb5fe9b86e");

    _initMaps();
    _listenMap();
    _tabController = TabController(vsync: this,length: 6);
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    AMapLocationClient.shutdown();
    super.dispose();
  }

  void _listenMap() {
    AMapLocationClient.onLocationUpate.listen((AMapLocation loc){
      if(!mounted)return;
      setState(() {
        if(loc.success){
          city = loc.city;
        }
        print(loc.formattedAddress);
      });
    });

    AMapLocationClient.startLocation();
  }

  void _initMaps() async {
    await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters  ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Row(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 26.5),
              width: 100.0,
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.add_location,
                    size: 24.0,
                  ),
                  Text(
                    "武侯区",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.0
                    ),
                  ),
                ],
              ),
            ),
            InkWell(
              highlightColor: Color(0x00000000),
              splashColor: Color(0x00000000),
              child: Container(
                margin: EdgeInsets.only(top: 26.5),
                height: 30.0,
                width: (deviceSize.width - 136.0),
                decoration: BoxDecoration(
                  color: Color(0x55CCCCCC),
                  border: Border.all(
                      color: Color(0xFFCCCCCC),
                      width: 1.0
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(15.0)),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 20.0),
                      child: Text(
                        "搜索广场动态……",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 15.0,

                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(right: 10.0),
                      child: Icon(
                        Icons.search,
                        color: Colors.white,
                      ),
                    ),
                  ],
                ),
              ),
              onTap: (){
                /// TODO 搜索
                print("搜索广场动态……");
              },
            ),
          ],
        ),
        backgroundColor: Color(0xffff7f15),
        bottom: TabBar(
          indicator: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                color: Colors.white,
                width: 2.0
              ),
            ),
          ),
          tabs: <Widget>[
            Tab(text: "热门"),
            Tab(text: "推荐"),
            Tab(text: "关注"),
            Tab(text: "收藏"),
            Tab(text: "新增"),
            Tab(text: "点赞"),
          ],
          controller: _tabController,  // 记得要带上tabController
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: <Widget>[
          Center(
              child: Text("这是热门的内容")
          ),
          Center(
              child: Text("这是推荐的内容")
          ),
          Center(
              child: Text("这是关注的内容")
          ),
          Center(
              child: Text("这是收藏的内容")
          ),
          Center(
              child: Text("这是新增的内容")
          ),
          Center(
              child: Text("这是点赞的内容")
          )
        ],
      ),
    );
  }
}